package com.xinda.admin.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xinda.admin.pojo.Admin;
import com.xinda.admin.pojo.AdminRole;
import com.xinda.admin.service.AdminService;
/**
 * 
	* <p>Title:AdminController </p>
	* <p>Description:对后台管理员的操作 </p>
	* <p>Company: </p> 
	* @author Administrator
	* @date 2017年8月23日下午3:55:40
 */
@Controller
public class AdminController {
	@Resource
	private AdminService adminService;
	

	/**
	 * 管理员列表
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "adminList")
	public String adminList(Model model,HttpSession httpSession,String adminName,String datemin,String datemax) {
		Map<String,String> conditionsMap = new HashMap<String,String>();
		conditionsMap.put("adminName", adminName);
		conditionsMap.put("datemin", datemin);
		conditionsMap.put("datemax", datemax);
		List<Admin> adminList = new ArrayList<Admin>();
		
		adminList = adminService.selectAllAdmin(conditionsMap);
		
		int countAdmin = adminService.countAdmin();
		model.addAttribute("countAdmin", countAdmin);
		model.addAttribute("adminList", adminList);
		model.addAttribute("adminName", adminName);
		model.addAttribute("datemin",datemin);
		model.addAttribute("datemax", datemax);
		return "admin/admin/adminList";
	}
	
	
	/**
	 * 管理员角色
	 * @return
	 */
	@RequestMapping(value ="adminRole")
	public String adminRole(Model model) {
		List<AdminRole> adminRoleList = new ArrayList<AdminRole>();
		
		adminRoleList = adminService.selectAllAdminRole();
		
		model.addAttribute("adminRoleList",adminRoleList);
		return "admin/admin/adminRole";
	}
	
	/**
	 * 添加管理员角色
	 * @return
	 */
	@RequestMapping(value ="adminRoleAdd")
	public String adminRoleAdd() {
		return "admin/admin/adminRoleAdd";
	}
	/**
	 * 添加管理员页面
	 * @return
	 */
	@RequestMapping(value ="adminAdd")
	public String adminAdd(Model model,Admin admin) {
		List<AdminRole> adminRoleList = new ArrayList<AdminRole>();
		
		adminRoleList = adminService.selectAllAdminRole();

		model.addAttribute("adminRoleList",adminRoleList);
		
		
		return "admin/admin/adminAdd";
	}
	/**
	 * 添加管理员
	 * @param model
	 * @return
	 */
	@RequestMapping(value ="adminAddSubmit")
	@ResponseBody
	public String adminAddSubmit(Model model,Admin admin) {
		
		Date registerTime = new Date();
		admin.setRegisterTime(registerTime);
		adminService.insertAdmin(admin);
		
		return "";
	}
	
	/**
	 * 编辑管理员页面
	 * @return
	 */
	@RequestMapping(value ="adminEdit")
	public String adminEdit(Integer id,Model model) {
		List<AdminRole> adminRoleList = new ArrayList<AdminRole>();
		adminRoleList = adminService.selectAllAdminRole();

		model.addAttribute("adminRoleList",adminRoleList);
		Admin admin = adminService.selectAdminById(id);
		model.addAttribute("admin", admin);
		return "admin/admin/adminEdit";
	}
	
	/**
	 * 编辑管理员
	 * @param model
	 * @return
	 */
	@RequestMapping(value ="adminEditSubmit")
	@ResponseBody
	public String adminEditSubmit(Model model,Admin admin) {
		
		
		adminService.editAdmin(admin);
		
		return "";
	}
	
	/**
	 * 删除管理员
	 * @return
	 */
	@RequestMapping(value ="adminDel")
	@ResponseBody
	public String adminDel(Integer id) {
		adminService.delAdminById(id);
		
		return "";
	}
	
	/**
	 * 批量删除管理员
	 */
	@RequestMapping(value ="adminsDel")
	@ResponseBody
	public String adminsDel(String id) {
		String[] ids = id.split(",");
		adminService.delAdmins(ids);
		return "";
	}
	
	/**
	 * 停用管理员
	 */
	@RequestMapping(value ="adminStop")
	@ResponseBody
	public String adminStop(Integer id) {
		
		adminService.stopAdmin(id);
		return "";
	}
	
	/**
	 * 启用管理员
	 */
	@RequestMapping(value ="adminStart")
	@ResponseBody
	public String adminStart(Integer id) {
		
		adminService.startAdmin(id);
		return "";
	}
	
}
